package com.lian.dao.impl;

import com.lian.dao.userdao;
import com.lian.entity.user;
import com.lian.util.DataBase;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2018/8/6.
 */
public class userdaoimpl implements userdao
{
    @Override
    //查询安装员名字 新增加载处理数据
    public List<user> find() {
        List<user>list=new ArrayList<user>();
        Connection conn= DataBase.getConnection();
        PreparedStatement stat=null;
        ResultSet rs=null;
        String sql="select * from user";
        try {
            stat = conn.prepareStatement(sql);

            rs = stat.executeQuery();
            while(rs.next()){
                user us=new user();
                us.setUid(rs.getInt("uid"));
                us.setUname(rs.getString("uname"));
                us.setCreateTime(rs.getString("createTime"));
                list.add(us);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            DataBase.closeConnection(conn, stat, rs);
        }
        return list;
    }

    public List<user> findus(int uid) {
        List<user>list=new ArrayList<user>();
        Connection conn= DataBase.getConnection();
        PreparedStatement stat=null;
        ResultSet rs=null;
        String sql="select * from user where uid=?";
        try {
            stat = conn.prepareStatement(sql);
            stat.setInt(1, uid);
            rs = stat.executeQuery();
            while(rs.next()){
                    user us=new user();
                    us.setUid(rs.getInt("uid"));
                    us.setUname(rs.getString("uname"));
                    us.setCreateTime(rs.getString("createTime"));
                    list.add(us);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }finally{
            DataBase.closeConnection(conn, stat, rs);
        }
        return list;
    }
}
